<template>
	<view class="app">
		<view><u-toast ref="uToast" /></view>
		<view class="tips">
			<view style="color: white; font-size: 32rpx;">可远程对学生平板已安装应用进行缓存清理。</view>
		</view>
		
		<view class="body">
			<u-form :model="form" ref="uForm" >
				<u-form-item label="选择应用:" label-width="150rpx" prop="app_name" >
					<u-input v-model="form.app_name" type="select" @click="show=true" border placeholder="请选择已安装应用"/>
					<u-select label-name="app_name" value-name="app_package" v-model="show" :list="appList" @confirm="setAppPackage"></u-select>
				</u-form-item>
			</u-form>
			<view style="display: flex; justify-content: space-between; margin-top: 60rpx; margin-bottom: 60rpx;">
				<u-button @click="clearAppMemoryControl"  type="primary"  :custom-style="customStyle">清除缓存</u-button>
				<u-button @click="cancel"  :custom-style="customStyle">取消</u-button>
			</view>
		</view>
	</view>
		
</template>

<script>
import {clearAppMemory, getAppInstall } from '../../../api/userHome';

	export default {
		data() {
			return {
				form: {
					app_name: '',
					app_package: ''
				},
				appList: [],
				show: false,
				customStyle: {
					magin: '0rpx',
					padding: '0rpx',
					width: '300rpx',
					height: '80rpx'
				},
				rules: {
					app_name: [{required: true, message: '请输入应用包名', trigger: 'change'}],
					app_package: [{required: true, message: '请选择应用', trigger: 'blur'}]
				}
			}
		},
		onLoad() {
			this.queryInstallApp()
		},
		methods: {
			
			// 点击actionSheet回调
			setAppPackage(e) {
				this.form.app_package = e[0].value
				this.form.app_name = e[0].label
				
			},
			queryInstallApp() {
				getAppInstall({
					id: uni.getStorageSync("snId"),
					userId: uni.getStorageSync("userId"),
					pageSize: 999
				}).then(res => {
					this.appList = res.data
				}).catch(err => {
					this.appList = []
				})
			},
			clearAppMemoryControl() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						let addForm = JSON.parse(JSON.stringify(this.form))
						addForm.userId = uni.getStorageSync('userId')
						addForm.id = uni.getStorageSync('snId')
						addForm.last_ip = uni.getStorageSync('Ip')
						clearAppMemory(addForm).then(res => {
							this.$refs.uToast.show({
								title: res.msg,
								type: 'success',
								icon: false
							})
						}).catch(err => {
							this.$refs.uToast.show({
								title: err.msg,
								type: 'error',
								icon: false
							})
						})
					}else {
						console.log('验证失败')
					}
				})
			},
			cancel() {
				console.log('取消提交')
				this.form = this.$options.data().form
			}
			
		}
	}
	
</script>

<style lang="scss">
	.app {
		.tips {
			    background-color: #4d94ff;
			    border-radius: 35rpx;
			    height: 70rpx;
			    padding-left: 60rpx;
			    padding-top: 13rpx;
		}
		
		.body {
			background-color: #ffffff;
			border-style: solid;
			border-color: #ffffff;
			border-radius: 20rpx;
			margin: 15rpx;
			padding: 10rpx;
		}
	}
</style>